package com.hqyj.springCloundAccount.dao;

;
import com.hqyj.account.entity.Resource;
import com.hqyj.commom.vo.Search;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface ResourceDao2 {

    @Insert("insert into test_resource (create_date,update_date,permition) values (#{createDate},#{updateDate},#{permition})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void addResource(Resource resource);

    @Update("update test_resource set update_date = #{updateDate} and permition = #{permition} where id = #{id}")
    void updateResource(Resource resource);

    @Delete("delete from test_resource where id = #{id}")
    void deleteResourceById(int id);

    @Select("select * from test_resource where id = #{id}")
    @Results(id = "resourceResult",value = {
            @Result(column = "id",property = "id"),
            @Result(column = "id",property = "roles",
            javaType = List.class,
            many = @Many(select = "com.hqyj.springboot.modules.account.dao.RoleDao.getRolesByResourceId"))
    })
    Resource getResourceById(int id);

    @Select("select * from test_resource as tr left join test_role_resource as trr on tr.id = trr.resource_id where trr.role_id = #{roleId}")
    List<Resource> getResourcesByRoleId(int roleId);

    @Select("<script>"
            + "select * from test_resource "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (permition like '%${pageSize}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='sort != \"\" and sort != null'>"
            + " order by ${sort} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<Resource> getResourcesBySearch(Search search);

}
